<?php
class loginAction extends frontAction{
	public function execute(){
		$id = Tool::_getSession('userID');
		if(!$id){
			$code = $this->getGetParam('code','');
			if($code!=''){
				$params = array();
				$params['code'] = $code;
				$params['redirect_uri'] = rare_httpHost().url('user/login');
				$token = service_weibo::getAccessToken('code',$params);
				$user = service_weibo::getUserInfo($token['access_token'],$token['uid']);
				if(!empty($user)){
					$userParams = array();
					$userParams['wImageUrl'] = $user['profile_image_url'];
					$userParams['wScreenName'] = $user['screen_name'];
					$userParams['wName'] = $user['name'];
					$userParams['wUrl'] = $user['profile_url'];
					$userParams['gender'] = $user['gender'];
					$userID = service_user::update($user['id'],$userParams);
					if($userID){
						Tool::_setSession('userID',$userID);
						redirect(url('user/index'));
						exit;
					}
				}
			}
			redirect(service_weibo::getAuthorizeUrl(rare_httpHost().url('user/login')));
			exit;
		}
	}

}
?>
